#pragma once

#include "vv_log.h"
#include <string.h>
#define EXTRACT_FILENAME(path) (strrchr((path), '/') ? strrchr((path), '/') + 1 : (path))

extern VVLOGHANDLE g_vv_xxxx123asdasdasd_log_handle;
#define vvlog_init(log_path, log_module, level) do { \
    if (!g_vv_xxxx123asdasdasd_log_handle) \
        g_vv_xxxx123asdasdasd_log_handle = vv_log_create(log_path, log_module, level); \
    } while(0)

#define vv_trace(fmt, ...) do { \
    int iFileWidth = 15,iFunWidth=25;\
    const char *pfileName=EXTRACT_FILENAME(__FILE__);\
    vv_log_trace(g_vv_xxxx123asdasdasd_log_handle, "[%*s:%*s:%d]: " \
    fmt,iFileWidth,pfileName,iFunWidth,__func__, __LINE__,##__VA_ARGS__);\
    } while(0)

#define vv_debug(fmt, ...) do { \
    int iFileWidth = 15,iFunWidth=25;\
    const char *pfileName=EXTRACT_FILENAME(__FILE__);\
    vv_log_debug(g_vv_xxxx123asdasdasd_log_handle, "[%*s:%*s:%d]: " \
    fmt,iFileWidth,pfileName,iFunWidth,__func__, __LINE__,##__VA_ARGS__);\
    } while(0)

#define vv_info(fmt, ...) do { \
    int iFileWidth = 15,iFunWidth=25;\
    const char *pfileName=EXTRACT_FILENAME(__FILE__);\
    vv_log_info(g_vv_xxxx123asdasdasd_log_handle, "[%*s:%*s:%d]: " \
    fmt,iFileWidth,pfileName,iFunWidth,__func__, __LINE__,##__VA_ARGS__);\
    } while(0)

#define vv_warn(fmt, ...) do { \
    int iFileWidth = 15,iFunWidth=25;\
    const char *pfileName=EXTRACT_FILENAME(__FILE__);\
    vv_log_warn(g_vv_xxxx123asdasdasd_log_handle, "[%*s:%*s:%d]: " \
    fmt,iFileWidth,pfileName,iFunWidth,__func__, __LINE__,##__VA_ARGS__);\
    } while(0)

#define vv_error(fmt, ...) do { \
    int iFileWidth = 15,iFunWidth=25;\
    const char *pfileName=EXTRACT_FILENAME(__FILE__);\
    vv_log_error(g_vv_xxxx123asdasdasd_log_handle, "[%*s:%*s:%d]: " \
    fmt,iFileWidth,pfileName,iFunWidth,__func__, __LINE__,##__VA_ARGS__);\
    } while(0)

#define vv_critical(fmt, ...) do { \
    int iFileWidth = 15,iFunWidth=25;\
    const char *pfileName=EXTRACT_FILENAME(__FILE__);\
    vv_log_critical(g_vv_xxxx123asdasdasd_log_handle, "[%*s:%*s:%d]: " \
    fmt,iFileWidth,pfileName,iFunWidth,__func__, __LINE__,##__VA_ARGS__);\
    } while(0)

#define vv_log(level, fmt, ...) do { \
    int iFileWidth = 15,iFunWidth=25;\
    const char *pfileName=EXTRACT_FILENAME(__FILE__);\
    vv_log_func(g_vv_xxxx123asdasdasd_log_handle, level, "[%*s:%*s:%d]: " \
    fmt,iFileWidth,pfileName,iFunWidth,__func__, __LINE__,##__VA_ARGS__);\
    } while(0)

#define vv_return_if_fail(x) do { \
    if (!x) { \
        vv_critical("condition '%s' failed", #x); \
        return; \
    } \
} while (0)

#define vv_return_val_if_fail(x, val) do { \
    if (!x) { \
        vv_critical("condition '%s' failed", #x);        \
        return (val); \
    }  \
} while (0)


#define vvlog_release() do { \
    if (g_vv_xxxx123asdasdasd_log_handle) { \
        vv_log_destroy(g_vv_xxxx123asdasdasd_log_handle); \
        g_vv_xxxx123asdasdasd_log_handle = NULL; \
    } \
} while(0)
